package com.sibu.yx.promotion.web.support.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.PriorityOrdered;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.doubo.common.constants.CommonConstants;
import com.sibu.yx.promotion.web.support.mvc.CustomServletModelAttributeMethodProcessor;

/**
 * mvc添加配置
 * @author yuwenbo
 * @create 2019-04-02 17:52
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer, PriorityOrdered {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //registry.addInterceptor(new SerialNoInitInterceptor());
    }


    /*
     * @description: 添加参数解析
     * @param: [resolvers]
     * @return: void
     * @author: yuwenbo@wljs.com
     * @date:2020-06-05
     * @time:16:27
     **/
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new CustomServletModelAttributeMethodProcessor());
    }

    //
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        HttpMessageConverters httpMessageConverters=new HttpMessageConverters(getConverters());
        converters.addAll(httpMessageConverters.getConverters());
    }

    @Bean
    public InternalResourceViewResolver jspViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".jsp");
        resolver.setOrder(1);
        resolver.setPrefix("/");
        resolver.setContentType("text/html;charset=UTF-8");
        return resolver;
    }


   /* @Override
    public void configureViewResolvers(ViewResolverRegistry registry){
        registry.order(1);
        registry.jsp("/view/",".jsp");
    }
*/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
       registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
       registry.addResourceHandler("/docs.html").addResourceLocations("classpath:/META-INF/resources/");
       registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    private List<HttpMessageConverter<?>> getConverters() {
        List<HttpMessageConverter<?>> myConverters = new ArrayList<>();
        myConverters.add(getJsonMsgConverter());
        myConverters.add(getStrMsgConverter());
        myConverters.add(getByteArrConverter());
        return myConverters;
    }



    private HttpMessageConverter<Object> getJsonMsgConverter() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        fastConverter.getFastJsonConfig().setSerializerFeatures(
                // 保留map空的字段
                SerializerFeature.WriteMapNullValue,
                // 将String类型的null转成""
                SerializerFeature.WriteNullStringAsEmpty,
                // 将Number类型的null转成0
                SerializerFeature.WriteNullNumberAsZero,
                // 将List类型的null转成[]
                SerializerFeature.WriteNullListAsEmpty,
                // 将Boolean类型的null转成false
                SerializerFeature.WriteNullBooleanAsFalse,
                // 避免循环引用
                SerializerFeature.DisableCircularReferenceDetect,
                // 格式化日期
                SerializerFeature.WriteDateUseDateFormat
        );
        fastConverter.setSupportedMediaTypes(Arrays.asList(
                MediaType.APPLICATION_JSON_UTF8,
                MediaType.valueOf("application/x-www-form-urlencoded; charset=UTF-8"),
                MediaType.valueOf("text/plain;charset=UTF-8"),
                MediaType.valueOf("text/html;charset=UTF-8")
        ));
        return fastConverter;
    }

    private HttpMessageConverter<String> getStrMsgConverter() {
        return new StringHttpMessageConverter(CommonConstants.UTF8_CHARSET);
    }

    private HttpMessageConverter<byte[]> getByteArrConverter() {
        return new ByteArrayHttpMessageConverter();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
